<template>
  <div class="cart-item">
    <!-- 选中框 -->
    <div class="item-checkbox">
      <el-checkbox 
        v-model="isSelected" 
        @change="handleSelectChange"
        :disabled="!canSelect"
      />
    </div>
    
    <!-- 商品信息 -->
    <div class="item-info">
      <router-link :to="`/product/detail?id=${item.id}`" class="item-image">
        <img :src="item.image || '/default-product.png'" :alt="item.name" class="product-img">
      </router-link>
      
      <div class="item-details">
        <router-link :to="`/product/detail?id=${item.id}`" class="item-name">
          {{ item.name }}
        </router-link>
        
        <!-- 商品规格 -->
        <div v-if="item.specs && item.specs.length > 0" class="item-specs">
          <div v-for="spec in item.specs" :key="spec.name" class="spec-item">
            <span class="spec-name">{{ spec.name }}:</span>
            <span class="spec-value">{{ spec.value }}</span>
          </div>
        </div>
        
        <!-- 商品单价 -->
        <div class="item-price">
          单价：¥{{ formatPrice(item.price) }}
        </div>
      </div>
    </div>
    
    <!-- 商品数量 -->
    <div class="item-quantity">
      <el-input-number 
        v-model="item.quantity" 
        :min="1" 
        :max="item.stock || 999" 
        @change="handleQuantityChange"
        size="small"
      />
    </div>
    
    <!-- 商品小计 -->
    <div class="item-subtotal">
      <span class="subtotal-price">¥{{ formatPrice(item.price * item.quantity) }}</span>
    </div>
    
    <!-- 操作按钮 -->
    <div class="item-actions">
      <button 
        @click="handleRemove" 
        class="action-btn remove-btn"
        :disabled="!canRemove"
      >
        <el-icon><Delete /></el-icon>
        <span>删除</span>
      </button>
      
      <button 
        @click="handleMoveToFavorites" 
        class="action-btn favorite-btn"
        :disabled="!canMoveToFavorites"
      >
        <el-icon><Star /></el-icon>
        <span>收藏</span>
      </button>
    </div>
    
    <!-- 商品状态标签 -->
    <div v-if="item.status && item.status !== 'normal'" class="item-status">
      <span class="status-tag" :class="`status-${item.status}`">
        {{ getStatusText(item.status) }}
      </span>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useCartStore } from '../../store/modules/cart'
import { Delete, Star } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { formatPrice } from '../../utils/helper'

const props = defineProps({
  // 购物车商品项数据
  item: {
    type: Object,
    required: true,
    default: () => ({})
  },
  // 是否可选中
  canSelect: {
    type: Boolean,
    default: true
  },
  // 是否可删除
  canRemove: {
    type: Boolean,
    default: true
  },
  // 是否可移动到收藏
  canMoveToFavorites: {
    type: Boolean,
    default: true
  },
  // 是否显示数量控制
  showQuantityControl: {
    type: Boolean,
    default: true
  },
  // 是否显示操作按钮
  showActions: {
    type: Boolean,
    default: true
  }
})

const cartStore = useCartStore()
const isSelected = ref(props.item.selected || false)

// 监听外部选中状态变化
watch(() => props.item.selected, (newVal) => {
  isSelected.value = newVal
})

// 处理选中状态变化
const handleSelectChange = (checked) => {
  cartStore.updateItemSelection(props.item.id, checked)
}

// 处理数量变化
const handleQuantityChange = (value) => {
  if (value < 1) value = 1
  if (props.item.stock && value > props.item.stock) {
    ElMessage.warning(`库存不足，最多只能购买${props.item.stock}件`)
    value = props.item.stock
  }
  
  cartStore.updateItemQuantity(props.item.id, value)
}

// 处理删除
const handleRemove = () => {
  cartStore.removeFromCart(props.item.id)
  ElMessage.success('商品已从购物车移除')
}

// 处理移动到收藏
const handleMoveToFavorites = () => {
  // 实际项目中这里应该调用收藏API
  ElMessage.success('商品已添加到收藏')
  // 可以选择是否从购物车中移除
  // cartStore.removeFromCart(props.item.id)
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    normal: '正常',
    outOfStock: '缺货',
    soonOut: '即将缺货',
    invalid: '已失效'
  }
  return statusMap[status] || status
}

// 监听数量变化，同步到父组件
const watchQuantity = () => {
  if (props.item.quantity !== isSelected.value) {
    cartStore.updateItemQuantity(props.item.id, props.item.quantity)
  }
}
</script>

<style scoped>
.cart-item {
  display: flex;
  align-items: center;
  padding: 16px;
  background-color: #fff;
  border-bottom: 1px solid $border-color;
  transition: background-color 0.3s;
}

.cart-item:hover {
  background-color: $bg-color;
}

/* 选中框 */
.item-checkbox {
  flex: 0 0 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 商品信息 */
.item-info {
  flex: 1;
  display: flex;
  gap: 16px;
  align-items: flex-start;
}

.item-image {
  flex: 0 0 80px;
  height: 80px;
  border-radius: $border-radius-base;
  overflow: hidden;
}

.product-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-details {
  flex: 1;
  min-width: 0;
}

.item-name {
  font-size: $font-size-base;
  color: $text-color;
  line-height: 1.4;
  margin-bottom: 8px;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  text-decoration: none;
}

.item-name:hover {
  color: $primary-color;
}

/* 商品规格 */
.item-specs {
  margin-bottom: 8px;
}

.spec-item {
  display: inline-block;
  margin-right: 12px;
  margin-bottom: 4px;
}

.spec-name {
  font-size: $font-size-xs;
  color: $text-color-placeholder;
}

.spec-value {
  font-size: $font-size-xs;
  color: $text-color-secondary;
}

/* 商品单价 */
.item-price {
  font-size: $font-size-sm;
  color: $text-color-secondary;
}

/* 商品数量 */
.item-quantity {
  flex: 0 0 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 商品小计 */
.item-subtotal {
  flex: 0 0 100px;
  text-align: right;
  padding-right: 20px;
}

.subtotal-price {
  font-size: $font-size-base;
  color: $danger-color;
  font-weight: bold;
}

/* 操作按钮 */
.item-actions {
  flex: 0 0 120px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-end;
}

.action-btn {
  padding: 6px 12px;
  border: 1px solid $border-color;
  background-color: #fff;
  color: $text-color;
  font-size: $font-size-sm;
  cursor: pointer;
  border-radius: $border-radius-base;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 4px;
}

.action-btn:hover:not(:disabled) {
  color: $primary-color;
  border-color: $primary-color;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.remove-btn:hover:not(:disabled) {
  color: $danger-color;
  border-color: $danger-color;
}

.favorite-btn:hover:not(:disabled) {
  color: $warning-color;
  border-color: $warning-color;
}

/* 商品状态标签 */
.item-status {
  flex: 0 0 80px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-tag {
  padding: 2px 8px;
  font-size: 12px;
  border-radius: $border-radius-small;
}

.status-outOfStock {
  background-color: $text-color-placeholder;
  color: #fff;
}

.status-soonOut {
  background-color: $warning-color;
  color: #fff;
}

.status-invalid {
  background-color: $danger-color;
  color: #fff;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .item-info {
    gap: 12px;
  }
  
  .item-image {
    flex: 0 0 60px;
    height: 60px;
  }
  
  .item-quantity {
    flex: 0 0 100px;
  }
  
  .item-subtotal {
    flex: 0 0 80px;
    padding-right: 10px;
  }
  
  .item-actions {
    flex: 0 0 100px;
  }
}

@media (max-width: 992px) {
  .cart-item {
    padding: 12px;
  }
  
  .item-name {
    font-size: $font-size-sm;
  }
  
  .item-price {
    font-size: $font-size-xs;
  }
  
  .subtotal-price {
    font-size: $font-size-sm;
  }
  
  .action-btn {
    padding: 4px 8px;
    font-size: $font-size-xs;
  }
}

@media (max-width: 768px) {
  .cart-item {
    flex-wrap: wrap;
    padding: 10px;
  }
  
  .item-checkbox {
    flex: 1 1 100%;
    justify-content: flex-start;
    margin-bottom: 10px;
  }
  
  .item-info {
    flex: 1 1 100%;
    margin-bottom: 10px;
  }
  
  .item-quantity,
  .item-subtotal,
  .item-actions {
    flex: 1 1 auto;
    justify-content: flex-start;
  }
  
  .item-actions {
    flex-direction: row;
    gap: 10px;
  }
}

@media (max-width: 480px) {
  .cart-item {
    padding: 8px;
  }
  
  .item-image {
    flex: 0 0 50px;
    height: 50px;
  }
  
  .item-actions {
    flex-direction: column;
    gap: 5px;
  }
}
</style>